2120. 执行所有后缀指令
为保证权益,题目请参考 2120. 执行所有后缀指令(From LeetCode).
解决方案1
Python
python
# 5964. 执行所有后缀指令
# https://leetcode-cn.com/problems/execution-of-all-suffix-instructions-staying-in-a-grid/
from typing import List
class Solution:
def executeInstructions(self, n: int, startPos: List[int], s: str) -> List[int]:
ans = [0] * len(s)
def dfs(i, pos):
if i == n or not (pos[0] >= 0 and pos[0] < n - 1 and pos[1] >= 0 and pos[1] < n - 1):
pass
for i in range(len(s)):
if ans[i] == -1:
dfs(i, startPos)
las_pos = [0, 0]
changes = []
for i in range(len(s)):
if s[i] == "L":
las_pos = [las_pos[0], las_pos[1] - 1]
elif s[i] == "R":
las_pos =[las_pos[0], las_pos[1] + 1]
elif s[i] == "U":
las_pos =[las_pos[0] - 1, las_pos[1]]
elif s[i] == "D":
las_pos =[las_pos[0] + 1, las_pos[1]]
changes.append(las_pos)
for i in range(len(s)):
pos = startPos[:]
for j in range(i, len(s)):
pos = [startPos[0] + changes[j][0], startPos[1] + changes[j][1]]
if i > 0:
pos = [pos[0] - changes[i-1][0], pos[1] - changes[i-1][1]]
if 0 <= pos[0] <= n - 1 and 0 <= pos[1] <= n - 1:
ans[i] += 1
else:
break
return ans
if __name__ == "__main__":
sol = Solution()
print(sol.executeInstructions(3, [0,1], "RRDDLU"))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48